// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-09-23  Jed Young  Creation
//

using chunmapView
using chunmapData
using fwt
using gfx
using slanData

class AttrTable
{
  Void show(Layer layer)
  {
    // model and table
    model := AttrModel(layer)
    table := Table { it.model = model; multi = true }

    // event handlers
    table.onAction.add |event|
    {
      echo("$event => " + model.text(0, event.index))
    }
    table.onSelect.add |event|
    {
      echo("$event => " + model.text(0, event.index))
    }
    table.onMouseDown.add |event|
    {
      row := table.rowAt(event.pos)
      col := table.colAt(event.pos)
      if (row != null && col != null)
        echo("$event => " + model.text(col, row))
      else
        echo("$event => null")
    }
    table.onPopup.add |event|
    {
      echo("$event => " + model.text(0, event.index))
      event.popup = Menu
      {
        MenuItem { text = model.text(0, event.index) },
      }
    }

    // put together the whole screen and open
    Window
    {
      title = "Attribute Table: $layer.name"
      EdgePane
      {
        center = table
      },;
      size = Size(800,600)
    }.open
  }
}


class AttrModel : TableModel
{
  FeatureSet set

  new make(VectorLayer layer)
  {
    this.set = layer.dataSource
  }

  override Str header(Int col) { set.schema.get(col).name }
  override Str text(Int col, Int row)
  {
    Record r:= set->features->get(row)
    return r.get(col).toStr
  }
  override Int numRows() { set->features->size }
  override Int numCols() { set.schema.size }
  override Halign halign(Int col) { Halign.center }
  override Color? fg(Int col, Int row) { Color.black }
}

